﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Security.Cryptography;
using Nomad.WebSync.Extensions;

namespace Nomad.WebSync.Pocket {
    class Settings {        
        public string Service { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public bool SyncContacts { get; set; }
        public bool SyncCalendar { get; set; }
        public bool SyncTasks { get; set; }
        public bool SyncNotes { get; set; }

        private string Filename {
            get {
                var application = Assembly.GetExecutingAssembly().GetName().CodeBase;
                return Path.ChangeExtension(application, ".dat");
            }
        }

        public Settings() {
            this.Username = String.Empty;
            this.Password = String.Empty;

            using (var service = new WebSyncService.Mobile()) {
                this.Service = service.Url;
            }

            this.Read();
        }

        public void Write() {
            if (File.Exists(this.Filename)) File.Delete(this.Filename);

            using (var file = File.Create(this.Filename)) {
                var bytes = this.ToBytes();
                file.Write(bytes, 0, bytes.Length);
                file.Close();
            }
        }
        private void Read() {
            if (!File.Exists(this.Filename)) return;

            using (var file = File.OpenRead(this.Filename)) {
                int length = Convert.ToInt32(file.Length);
                var bytes = new byte[length];
                
                file.Read(bytes, 0, bytes.Length);                
                file.Close();

                this.FromBytes(bytes);
            }
        }

        private byte[] ToBytes() {            
            var usernameBytes = this.Username.GetBytes();
            var passwordBytes = this.Password.GetBytes();
            var serviceBytes = this.Service.GetBytes();
            var syncContacts = Convert.ToByte(this.SyncContacts);
            var syncCalendar = Convert.ToByte(this.SyncCalendar);
            var syncTasks = Convert.ToByte(this.SyncTasks);
            var syncNotes = Convert.ToByte(this.SyncNotes);            

            var header = new byte[3];
            header[0] = Convert.ToByte(usernameBytes.Length);
            header[1] = Convert.ToByte(passwordBytes.Length);
            header[2] = Convert.ToByte(serviceBytes.Length);

            byte[] buffer;
            using (var memory = new MemoryStream()) {
                memory.Write(header, 0, header.Length);
                memory.Write(usernameBytes, 0, usernameBytes.Length);
                memory.Write(passwordBytes, 0, passwordBytes.Length);
                memory.Write(serviceBytes, 0, serviceBytes.Length);

                memory.WriteByte(syncContacts);
                memory.WriteByte(syncCalendar);
                memory.WriteByte(syncTasks);
                memory.WriteByte(syncNotes);

                buffer = memory.ToArray();
            }
            return buffer.Encrypt();
        }
        private void FromBytes(byte[] bytes) {            
            if (bytes.Length == 0)
                return;

            using (var memory = new MemoryStream(bytes.Decrypt())) {
                var header = new byte[3];                
                memory.Read(header, 0, header.Length);

                var usernameBytes = new byte[header[0]];
                memory.Read(usernameBytes, 0, usernameBytes.Length);
                this.Username = usernameBytes.GetString();

                var passwordBytes = new byte[header[1]];
                memory.Read(passwordBytes, 0, passwordBytes.Length);
                this.Password = passwordBytes.GetString();

                var serviceBytes = new byte[header[2]];
                memory.Read(serviceBytes, 0, serviceBytes.Length);
                this.Service = serviceBytes.GetString();

                this.SyncContacts = Convert.ToBoolean(memory.ReadByte());
                this.SyncCalendar = Convert.ToBoolean(memory.ReadByte());
                this.SyncTasks = Convert.ToBoolean(memory.ReadByte());
                this.SyncNotes = Convert.ToBoolean(memory.ReadByte());

                memory.Close();
            }
        }
    }
}
